package com.rc.evcharger.service.elec;

import com.rc.evcharger.common.config.FactoryDomain;
import com.rc.evcharger.common.config.RouterConfig;
import com.rc.evcharger.common.constant.SocketConstant;
import com.rc.evcharger.mapper.elec.ElecPileHostMapper;
import com.rc.evcharger.mapper.elec.ElecPileMapper;
import com.rc.evcharger.model.elec.ElecPile;
import com.rc.evcharger.model.elec.ElecPileHost;
import com.rc.evcharger.redis.RedChargeDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;

/**
 * Create by Yang on 2021/12/21
 */
@Service
public class ElecPileHostServiceImpl implements ElecPileHostService {
    private static Logger logger = LoggerFactory.getLogger(ElecPileHostServiceImpl.class);

    @Autowired
    private RouterConfig routerConfig;

    @Autowired
    private ElecPileHostMapper elecPileHostMapper;

    @Autowired
    private ElecPileMapper elecPileMapper;

    @Autowired
    private RedChargeDataService redChargeDataService;

    @Autowired
    private FactoryDomain factoryDomain;


    @Override
    public ElecPileHostMapper getMapper() {
        return elecPileHostMapper;
    }

    /**
     * 每次登陆更新桩通讯表
     *
     * @param elecPileId
     * @return
     */
    @Override
    public int updateHostByElecPileId(Long elecPileId) {
        ElecPileHost elecPileHost = elecPileHostMapper.selectByPrimaryKeyForMaster(elecPileId);
        ElecPile elecPile = elecPileMapper.selectByPrimaryKey(elecPileId);

        String applicationName = RouterConfig.APPLICATION_NAME;
        String pileApplicationName = redChargeDataService.getPileApplicationName(elecPile.getPileRtua(), SocketConstant.STR_CON1, SocketConstant.STR_CON0);

        //获取主机IP和项目名称
        String hostName = "";
        try {
            hostName = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        if (elecPileHost == null) {
            //新增4
            ElecPileHost newElecPileHost = new ElecPileHost();
            newElecPileHost.setElecPileId(elecPileId);                                  //桩名称
            newElecPileHost.setTenantCode(elecPile.getTenantCode());                    //所属运营商
            newElecPileHost.setHlhtOperatorId(elecPile.getHlhtOperatorId());            //第三方运营商作为设备开放的充电站（正向对接）,关联hlht_operator表
            newElecPileHost.setHlhtPileNo(elecPile.getHlhtPileNo());                    //第三方运营商作为设备开放的桩编号（正向对接）
            newElecPileHost.setElecStationId(elecPile.getElecStationId());              //站点ID
            newElecPileHost.setPileNo(elecPile.getPileNo());                            //桩编号
            newElecPileHost.setElecServerDomain(elecPile.getElecServerDomain() == null ? factoryDomain.getFACTORY_STD_DOMAIN() : elecPile.getElecServerDomain());        //桩品牌域
            newElecPileHost.setElecServerSpliter(elecPile.getElecServerSpliter());      //分流器
            newElecPileHost.setApplicationName(applicationName);//服务器名称
            newElecPileHost.setHostIp(hostName);                                        //服务器IP

            newElecPileHost.setCreatedTime(new Date());
            newElecPileHost.setLastUpdatedTime(new Date());

            elecPileHostMapper.insertSelective(newElecPileHost);

            //更新桩的redis应用名称
            redChargeDataService.putPileApplicationName(elecPile.getPileRtua(), SocketConstant.STR_CON1, SocketConstant.STR_CON0, applicationName);
        } else {
            logger.info("更新前桩对应应用服务器名称桩编号:{},桩逻辑地址:{},applicationName：{},elecpileHost:{}",
                    elecPile.getPileNo(), elecPile.getPileRtua(), applicationName, elecPileHost.getApplicationName());
            //如果相等则不更新，不相等则更新数据库和redis
            if (applicationName.equals(pileApplicationName)) {
                return 0;
            }
            //更新
            ElecPileHost newElecPileHost = new ElecPileHost();
            newElecPileHost.setElecPileId(elecPile.getElecPileId());
            newElecPileHost.setLastUpdatedTime(new Date());
            newElecPileHost.setHostIp(hostName);                                        //服务器IP
            newElecPileHost.setElecServerDomain(elecPile.getElecServerDomain() == null ? factoryDomain.getFACTORY_STD_DOMAIN() : elecPile.getElecServerDomain());        //桩品牌域
            newElecPileHost.setElecServerSpliter(elecPile.getElecServerSpliter());      //分流器
            newElecPileHost.setApplicationName(applicationName);                        //服务器名称

            elecPileHostMapper.updateByPrimaryKeySelectiveForMaster(newElecPileHost);

            //更新桩的redis应用名称
            redChargeDataService.putPileApplicationName(elecPile.getPileRtua(), SocketConstant.STR_CON1, SocketConstant.STR_CON0, applicationName);
        }


        return 0;
    }


}
